<template>
  <div class="login-container">
    <!-- 左侧系统信息区域 -->
    <div class="system-info">
      <div class="logo-container">
        <img src="/logo_02.png" alt="系统Logo" class="system-logo" />
      </div>
      <h1 class="system-title">面向东盟的多语言文本对齐系统</h1>
      <p class="system-description">专业的多语言文本处理平台，为中国-东盟合作提供智能化语言服务</p>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-form">
      <h2>用户登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button color="var(--primary)" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="register-link">
        <router-link to="/register">还没有账号？立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script setup name="UserLogin">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 调用登录API
        const response = await login(loginForm)
        
        // 保存token到localStorage
        localStorage.setItem('token', response.data.token)
        // 保存用户信息
        localStorage.setItem('user', JSON.stringify(response.data.user))
        
        ElMessage.success('登录成功')
        
        // 根据用户角色跳转到不同页面
        if (response.data.user.role === 'admin') {
          router.push('/admin/dashboard')
        } else {
          router.push('/user-home')
        }
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}

const resetForm = () => {
  loginFormRef.value.resetFields()
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background: var(--primary-light);
}

/* 左侧系统信息区域 */
.system-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px 20px 40px 40px;
  background: var(--primary-light);
  color: var(--primary);
}

.logo-container {
  margin-bottom: -1rem;
}

.system-logo {
  max-width: 20rem;
  height: auto;
}

.system-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 20px;
  text-align: center;
  line-height: 1.2;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.system-description {
  font-size: 1.1rem;
  line-height: 1.6;
  text-align: center;
  max-width: 400px;
  opacity: 0.95;
  font-weight: 300;
}

/* 右侧登录表单 */
.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  backdrop-filter: blur(10px);
  border-left: 1px solid var(--border);
}

.login-form h2 {
  color: var(--primary);
  margin-bottom: 30px;
  font-size: 1.8rem;
  font-weight: 600;
}

.login-form .el-form {
  width: 100%;
  height: 16rem;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.5);
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 32px var(--shadow);
}

.el-form-item {
  display: flex;
  --font-size: 14px;
  margin-bottom: 2rem;
  margin-top: 2rem;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  width: 100%;
  max-width: 400px;
}

.register-link a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: var(--primary-strong);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .system-info {
    flex: none;
    min-height: 40vh;
    padding: 30px 20px;
  }

  .system-title {
    font-size: 1.8rem;
  }

  .system-description {
    font-size: 1rem;
  }

  .login-form {
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
</style>